package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ElderlyClassDTO;
import com.api.shopapi.service.ActivityTypeService;
import com.api.shopapi.service.CommandCenterStaffRelService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 活动类型
 * */

@RestController
@RequestMapping("ActivityType")
public class ActivityTypeController extends BaseController{

    @Autowired
    @Qualifier("ActivityTypeServiceImplOne")
    private ActivityTypeService activityTypeService;

    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");

            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityType/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            Staff staff = staffService.getByStaffCode(staffCode);

            ActivityType activityType =new ActivityType();
            activityType.setActivityTypeCode(UUID.randomUUID().toString());
            activityType.setName(name);
            activityType.setSort(sort);
            activityType.setStatus(1);
            activityType.setGovernmentCode(staff.getGovernmentCode());
            activityType.setCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
            activityTypeService.insert(activityType);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityType/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            int status = jsonObject.getInteger("status");
            String activityTypeCode = jsonObject.getString("activityTypeCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityType/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityType activityType =activityTypeService.getByActivityTypeCode(activityTypeCode);
            activityType.setName(name);
            activityType.setSort(sort);
            activityType.setStatus(status);
            activityTypeService.update(activityType);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityType/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityType/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            List<ActivityType> list = activityTypeService.listByCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityType/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityType/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            PageInfo<ActivityType> pageInfo = activityTypeService.pageByCommandCenterCode(pageIndex,pageSize,commandCenterStaffRel.getCommandCenterCode());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityType/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
